//
//  loginVC.m
//  CMAccountBook
//
//  Created by 王开政 on 2018/10/30.
//  Copyright © 2018年 王开政. All rights reserved.
//

#import "loginVC.h"

@interface loginVC ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *phoneText;
@property(nonatomic,strong)UITextField *codeText;

@property(nonatomic,strong)UIButton *codeBut;

@property(nonatomic,strong)UIButton *cancelBtn;

@property(nonatomic,strong)UIButton *loginBtn;

@property(nonatomic,strong)UILabel *contentLabel;

@property(nonatomic,strong)UILabel *otherLoginLabel;

@property(nonatomic,strong)UIButton *QQBut;
@property(nonatomic,strong)UIButton *WeiXinBut;
@end

@implementation loginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(self.phoneText.frame.origin.x, self.phoneText.frame.origin.y +self.phoneText.frame.size.height, self.phoneText.frame.size.width, 1)];
    line1.backgroundColor = [UIColor colorWithHexString:@"#e6e6e6"];
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(self.codeText.frame.origin.x, self.codeText.frame.origin.y +self.codeText.frame.size.height, self.codeText.frame.size.width, 1)];
    line2.backgroundColor = [UIColor colorWithHexString:@"#e6e6e6"];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap1.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap1];
    
    
    [self.view addSubview:self.cancelBtn];
    [self.view addSubview:self.phoneText];
    [self.view addSubview:self.codeText];
    
    [self.view addSubview:self.codeBut];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.contentLabel];
    [self.view addSubview:self.otherLoginLabel];
    
    [self.view addSubview:self.QQBut];
    [self.view addSubview:self.WeiXinBut];
    
    [self.view addSubview:line1];
    [self.view addSubview:line2];
    
    // Do any additional setup after loading the view.
}
-(UITextField *)phoneText{
    
    if (!_phoneText) {
        
        _phoneText = [[UITextField alloc]initWithFrame:CGRectMake(50*kfit, SafeAreaTopHeight +50*kfit, ScreenWidth - 100*kfit, 50)];
        _phoneText.textColor = [UIColor colorWithHexString:@"#222222"];
        _phoneText.placeholder = @"输入手机号" ;
        _phoneText.font = [UIFont systemFontOfSize:14*kfit];
        _phoneText.returnKeyType = UIReturnKeyDone;
        _phoneText.delegate = self;
        _phoneText.clearButtonMode = UITextFieldViewModeAlways;
        _phoneText.keyboardType = UIKeyboardTypePhonePad;
        _phoneText.tag = 1001;
        
    }
    
    return _phoneText;
}

-(UITextField *)codeText{
    
    if (!_codeText) {
        
        _codeText = [[UITextField alloc]initWithFrame:CGRectMake(50*kfit, SafeAreaTopHeight +100*kfit, ScreenWidth - 100*kfit, 50)];
        _codeText.textColor = [UIColor colorWithHexString:@"#222222"];
        _codeText.placeholder = @"输入验证码" ;
        _codeText.font = [UIFont systemFontOfSize:14*kfit];
        _codeText.returnKeyType = UIReturnKeyDone;
        _codeText.delegate = self;
        _codeText.clearButtonMode = UITextFieldViewModeAlways;
        _codeText.keyboardType = UIKeyboardTypePhonePad;
        _codeText.tag = 1002;
        
    }
    
    return _codeText;
}
-(UIButton *)codeBut{
    
    if (!_codeBut) {
        _codeBut = [UIButton buttonWithType:UIButtonTypeCustom];
//        _codeBut.backgroundColor = [UIColor colorWithHexString:@"#00CC9A"];
        [_codeBut setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:0];
        [_codeBut setTitle:@"获取验证码" forState:0];
        _codeBut.titleLabel.font = [UIFont systemFontOfSize:12*kfit];
//        [_codeBut setTitleColor:[UIColor whiteColor] forState:0];
        _codeBut.frame = CGRectMake(ScreenWidth-130*kfit, SafeAreaTopHeight +113*kfit, 80*kfit, 24*kfit);
        [_codeBut addTarget:self action:@selector(codePress) forControlEvents:UIControlEventTouchUpInside];
        _codeBut.layer.cornerRadius = 3;
        _codeBut.layer.masksToBounds = YES;
        //          self.codeBut.backgroundColor = [UIColor greenColor];
    }
    
    return _codeBut;
}
-(UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(331*kfit, SafeAreaTopHeight+10*kfit, 24*kfit, 24*kfit)];
        [_cancelBtn setImage:[UIImage imageNamed:@"叉"] forState:0];
        [_cancelBtn addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(50*kfit, SafeAreaTopHeight +200*kfit, ScreenWidth - 100*kfit, 50)];
        _loginBtn.backgroundColor = [UIColor darkGrayColor];
        [_loginBtn setTitle:@"登陆" forState:0];
        [_loginBtn setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:0];
        _loginBtn.layer.cornerRadius = 9;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn addTarget:self action:@selector(loginPress) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}
-(UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(50*kfit, self.loginBtn.frame.origin.y+self.loginBtn.frame.size.height + 30*kfit, ScreenWidth - 100*kfit, 30*kfit)];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = [UIColor colorWithHexString:@"#8A8A8A"];
        _contentLabel.font = [UIFont systemFontOfSize:12*kfit];
        _contentLabel.text = @"点击登录即表示已阅读并同意《CM记账用户协议》与《CM记账隐私政策》";
    }
    return _contentLabel;
}
-(UILabel *)otherLoginLabel{
    if (!_otherLoginLabel) {
        _otherLoginLabel = [[UILabel alloc]initWithFrame:CGRectMake(0*kfit, ScreenHeight -  140*kfit, ScreenWidth,20*kfit)];
        _otherLoginLabel.textAlignment = NSTextAlignmentCenter;
//        _otherLoginLabel.numberOfLines = 0;
        _otherLoginLabel.textColor = [UIColor colorWithHexString:@"#8A8A8A"];
        _otherLoginLabel.font = [UIFont systemFontOfSize:12*kfit];
        _otherLoginLabel.text = @"其他登录方式";
    }
    return _otherLoginLabel;
}
-(UIButton *)QQBut{
    
    if (!_QQBut) {
        
        _QQBut = [UIButton buttonWithType:UIButtonTypeCustom];
        _QQBut.frame = CGRectMake(132*kfit,ScreenHeight -  95*kfit,24*kfit, 24*kfit);
        [_QQBut addTarget:self action:@selector(qqPress) forControlEvents:UIControlEventTouchUpInside];
        [_QQBut setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal];
        
    }
    
    
    return  _QQBut;
}
-(UIButton *)WeiXinBut{
    
    if (!_WeiXinBut) {
        
        _WeiXinBut = [UIButton buttonWithType:UIButtonTypeCustom];
       _WeiXinBut.frame = CGRectMake(220*kfit,ScreenHeight -  95*kfit,24*kfit, 24*kfit);
        [_WeiXinBut addTarget:self action:@selector(WeiXinPress) forControlEvents:UIControlEventTouchUpInside];
        [_WeiXinBut setImage:[UIImage imageNamed:@"wechat"] forState:UIControlStateNormal];
        
    }
    
    
    return  _WeiXinBut;
}
-(void)cancelPress{
    
    NJLog(@"点击取消");
}
-(void)loginPress{
    
    NJLog(@"点击登录");
}
-(void)qqPress{
    
    NJLog(@"点击QQ登陆");
}
-(void)WeiXinPress{
    
    NJLog(@"点击微信登陆");
}
#pragma mark - 获取验证码
-(void)codePress{
    [self time];
//    pulicMath *math = [[pulicMath alloc]init];
//    NSString *str = getSMSForAuthLoginURL;
//
//    if ([math isBlankString:self.phoneText.text]) {
//
//        if ([math valiMobile:self.phoneText.text]) {
//
//            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.phoneText.text,@"phone", nil];
//            NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithCapacity:1];
//
//            dataDic = [math getData:str dic:dic];
//
//            if ([[dataDic allKeys] containsObject:@"errorMsg"]) {
//
//                [hdView showErrorViewMessage:dataDic[@"errorMsg"]];
//
//
//            }else{
//                if ([dataDic[@"info"]isEqualToString:@"USER_NONEXISTENCE"]) {
//                    self.invitationCodeView.hidden = NO;
//                }
//                else
//                {
//                    self.invitationCodeView.hidden = YES;
//                }
//                [self time];
//                [hdView showSuccessViewMessage:@"短信已发送请注意查收"];
//
//            }
//
//        } else {
//
//            [hdView showErrorViewMessage:@"请输入正确的手机号"];
//
//        }
//    }else{
//
//        [hdView showErrorViewMessage:@"手机号不能为空"];
//
//    }
}

#pragma mark - 验证码 倒计时 计时器

-(void)time{
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [self.codeBut setTitle:@"重新发送" forState:0];
                self.codeBut.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [self.codeBut setTitle:[NSString stringWithFormat:@"%.2ds", seconds] forState:0];
                self.codeBut.userInteractionEnabled = NO;
            });
            
            time--;
        }
    });
    
    
    dispatch_resume(_timer);
    //                    self.thread = [[NSThread alloc]initWithTarget:self selector:@selector(postData) object:nil];
    //                    [self.thread start];
    
}

#pragma mark-点击屏幕消失键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
    [self.view endEditing:YES];
    
}
#pragma marks UITextFieldDelegate
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
//    [self loginPress];
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.phoneText) {
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        NSInteger pointLength = existedLength - selectedLength + replaceLength;
        //超过20位 就不能在输入了
        if (pointLength > 11) {
            return NO;
        }else{
            return YES;
        }
        
    }else if(textField == self.codeText){
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        NSInteger pointLength = existedLength - selectedLength + replaceLength;
        //超过20位 就不能在输入了
        if (pointLength > 6) {
            return NO;
        }else{
            return YES;
        }
    }
  
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
